package com.a214.yxz.yuejian013.Activity;

import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.a214.yxz.yuejian013.Base.BaseSActivity;
import com.a214.yxz.yuejian013.JavaBean.ShaireplyBean;
import com.a214.yxz.yuejian013.JavaBean.ShaituBean;
import com.a214.yxz.yuejian013.Listener.ServiceListener;
import com.a214.yxz.yuejian013.Model.ResModel;
import com.a214.yxz.yuejian013.R;
import com.a214.yxz.yuejian013.Adapter.ShowCommentAdapter;
import com.squareup.picasso.Picasso;

import java.util.List;


public class ShowDetailActivity extends BaseSActivity {

    private ImageButton return_btn;
    private ImageView userPic;
    private ImageView showPic;
    private TextView userName, uploadTime, clubName, picIntro;
    private EditText reply_edit;
    private Button reply_btn;
    private ServiceListener<ShaituBean> shaitu_listener;
    private ServiceListener<List<ShaireplyBean>> reply_listener;
    private ServiceListener<String> creatReply_listener;
    private ResModel c_model=new ResModel("",getSessionID());
    private ResModel model=new ResModel("gson",getSessionID());
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;
    private ShowCommentAdapter adapter;
    private String shaitu_id,shaier_id;

    private String[] path;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_detail);

        return_btn= (ImageButton) findViewById(R.id.show_detail_return_btn);
        userPic= (ImageView) findViewById(R.id.show_detail_userPic);
        showPic= (ImageView) findViewById(R.id.show_detail_showPic);
        userName= (TextView) findViewById(R.id.show_detail_userName);
        uploadTime= (TextView) findViewById(R.id.show_detail_uploadTime);
        clubName= (TextView) findViewById(R.id.show_detail_clubName);
        picIntro= (TextView) findViewById(R.id.show_detail_picIntro);
        recyclerView= (RecyclerView) findViewById(R.id.show_detail_commentList);
        reply_edit= (EditText) findViewById(R.id.show_detail_reply_edit);
        reply_btn= (Button) findViewById(R.id.show_detail_reply_btn);
        shaitu_id=getIntent().getStringExtra("show_id");



        adapter=new ShowCommentAdapter(ShowDetailActivity.this);
        layoutManager=new LinearLayoutManager(ShowDetailActivity.this);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(layoutManager);


        reply_listener=new ServiceListener<List<ShaireplyBean>>() {
            @Override
            public void onResponse(List<ShaireplyBean> l) {
                adapter.setList(l);
            }

            @Override
            public void onFail(String msg) {
                Toast.makeText(ShowDetailActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
        };
        shaitu_listener=new ServiceListener<ShaituBean>() {
            @Override
            public void onResponse(ShaituBean l) {
                userPic.setImageBitmap(BitmapFactory.decodeResource(ShowDetailActivity.this.getResources(), R.drawable.user));
                showPic.setImageBitmap(BitmapFactory.decodeResource(ShowDetailActivity.this.getResources(), R.drawable.user));
                userName.setText(l.getShaier().getUser_name());
                uploadTime.setText(l.getUpdate_time());
                clubName.setText(l.getClub_name());
                picIntro.setText(l.getTitle());
                shaier_id=l.getUserid();


                Picasso.with(ShowDetailActivity.this)
                        .load("http://172.24.10.175/workout/Uploads/" + l.getThumb0())
                        .placeholder(R.drawable.email)
                        .into(showPic);
                Picasso.with(ShowDetailActivity.this)
                        .load("http://172.24.10.175/workout/Uploads/" + l.getShaier().getPhoto())
                        .placeholder(R.drawable.email)
                        .into(userPic);
            }

            @Override
            public void onFail(String msg) {
                Toast.makeText(ShowDetailActivity.this,msg, Toast.LENGTH_SHORT).show();
            }
        };
        creatReply_listener=new ServiceListener<String>() {
            @Override
            public void onResponse(String l) {
                Toast.makeText(ShowDetailActivity.this, "发布成功", Toast.LENGTH_SHORT).show();
                getReply();
            }

            @Override
            public void onFail(String msg) {
                //Toast.makeText(ShowDetailActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
        };

        model.getShaituDetail(shaitu_id,shaitu_listener);
        getReply();
        return_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        reply_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                c_model.createReply(shaitu_id,userId,shaier_id,reply_edit.getText().toString(),creatReply_listener);
                reply_edit.setText("");
            }
        });
    }
    private void getReply(){model.getShaireply(shaitu_id,reply_listener);}
}
